package cn.iocoder.yudao.module.dlbz.controller.admin.companyemployee;

import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dlbz.controller.admin.companyemployee.vo.*;
import cn.iocoder.yudao.module.dlbz.dal.dataobject.companyemployee.CompanyEmployeeDO;
import cn.iocoder.yudao.module.dlbz.service.companyemployee.CompanyEmployeeService;

@Tag(name = "管理后台 - 企业员工")
@RestController
@RequestMapping("/dlbz/company-employee")
@Validated
public class CompanyEmployeeController {

    @Resource
    private CompanyEmployeeService companyEmployeeService;

    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建企业员工")
    @PreAuthorize("@ss.hasPermission('dlbz:company-employee:create')")
    public CommonResult<Long> createCompanyEmployee(@Valid @RequestBody CompanyEmployeeSaveReqVO createReqVO) {

        //新建一个用户并且分配用户角色
        adminUserApi.createUserAndAssignRole(createReqVO.getPhone(), createReqVO.getCompanyId(),146, "employee");

        return success(companyEmployeeService.createCompanyEmployee(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新企业员工")
    @PreAuthorize("@ss.hasPermission('dlbz:company-employee:update')")
    public CommonResult<Boolean> updateCompanyEmployee(@Valid @RequestBody CompanyEmployeeSaveReqVO updateReqVO) {
        companyEmployeeService.updateCompanyEmployee(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除企业员工")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dlbz:company-employee:delete')")
    public CommonResult<Boolean> deleteCompanyEmployee(@RequestParam("id") Long id) {
        companyEmployeeService.deleteCompanyEmployee(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得企业员工")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dlbz:company-employee:query')")
    public CommonResult<CompanyEmployeeRespVO> getCompanyEmployee(@RequestParam("id") Long id) {
        CompanyEmployeeDO companyEmployee = companyEmployeeService.getCompanyEmployee(id);
        return success(BeanUtils.toBean(companyEmployee, CompanyEmployeeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得企业员工分页")
    @PreAuthorize("@ss.hasPermission('dlbz:company-employee:query')")
    public CommonResult<PageResult<CompanyEmployeeRespVO>> getCompanyEmployeePage(@Valid CompanyEmployeePageReqVO pageReqVO) {
        PageResult<CompanyEmployeeDO> pageResult = companyEmployeeService.getCompanyEmployeePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CompanyEmployeeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出企业员工 Excel")
    @PreAuthorize("@ss.hasPermission('dlbz:company-employee:export')")
    @OperateLog(type = EXPORT)
    public void exportCompanyEmployeeExcel(@Valid CompanyEmployeePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CompanyEmployeeDO> list = companyEmployeeService.getCompanyEmployeePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "企业员工.xls", "数据", CompanyEmployeeRespVO.class,
                        BeanUtils.toBean(list, CompanyEmployeeRespVO.class));
    }

}